package controllers

import (
	"blog/models"

	"github.com/astaxie/beego"
	"github.com/astaxie/beego/context"
)

type MainController struct {
	beego.Controller
}

func (c *MainController) Get() {
	if checkAccount(c.Ctx) {
		c.Data["Cookie"] = true
	}

	var err error
	c.Data["fenlei"], err = models.GetFenleis()
	if err != nil {
		beego.Error(err)
	}
	c.Data["ToPics"], err = models.GetTopics()
	if err != nil {
		beego.Error("get topic error!")
	}
	c.Data["Website"] = "beego.me"
	c.Data["Email"] = "1034552569@qq.com"
	c.TplName = "index.html"
}

func checkAccount(ctx *context.Context) bool {

	username := ctx.GetCookie("username")
	pwd := ctx.GetCookie("password")

	return beego.AppConfig.String("username") == username &&
		beego.AppConfig.String("userpassword") == pwd
}
